<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class SignUp extends CI_Controller {
        public function index(){
            $this->load->view('user_group3/login');
        }
        
        public function Pre_Signup(){
            try{
                $uniqid = md5(uniqid($_POST['email']));
                $array = array("'".$_POST['email']."'", 
                    "'".$_POST['password']."'", 
                    "'".$uniqid."'",
                    "'".$_POST['looking']."'"
                    );
                $string = implode(",",$array);
                $query = $this->db->query(""
                        . "INSERT INTO ".MYSQL_TABLE_PREFLIX.TABLE_TEMP_USER_ACCOUNTS." "
                        . "VALUES (".$string.")");

                $this->load->helper('email');
                $Content = "Dear ".$_POST['email']
                        .". Please click the link below to confirm your registrationL <br><br>"
                        ."<a href='".site_url().'/user_group3/signup/confirm_signup/'.$uniqid."'>Confirmation Link</a>"
                        . "<br><br> or copy this <br><br>"
                        . site_url().'/user_group3/signup/confirm_signup/'.$uniqid;
                $Subject = "Please confirm your registration!";
                sendmail($_POST['email'], $Subject, $Content);
            }
            catch(Exception $e){
               exit;
            }
           $this->load->view('confirm_email',$_POST);

        }
        
        public function Verify_Email($Email){
            $Email = urldecode($Email);
            $query = $this->db->query(""
                    . "Select * "
                    . "From ".MYSQL_TABLE_PREFLIX.TABLE_USER_ACCOUNTS." "
                    . "Where UserEmail = '".$Email."'") ;
            $query2 = $this->db->query(""
                    . "Select * "
                    . "From ".MYSQL_TABLE_PREFLIX.TABLE_TEMP_USER_ACCOUNTS." "
                    . "Where UserEmail = '".$Email."'") ;
            if($query->num_rows() > 0 || $query2->num_rows() > 0)
            {
                echo "0";
            }
            else{
                echo "1";
            }
        }
        
        public function Confirm_Signup($UniId){
            $query = $this->db->query(""
                    . "Select * "
                    . "From ".MYSQL_TABLE_PREFLIX.TABLE_TEMP_USER_ACCOUNTS." "
                    . "Where ConfirmCode = '".$UniId."'") ;
            if($query->num_rows > 0)
            {
                $query = $query->result_array();
                $query = $query[0];
                $array = array("'".$query['UserEmail']."'", 
                "'".$query['UserPassword']."'", 
                "'"."0"."'",
                "'".$query['UserType']."'"
                );
                $string = implode(",",$array);
                $this->db->query(""
                            . "INSERT INTO ".MYSQL_TABLE_PREFLIX.TABLE_USER_ACCOUNTS." "
                            . "VALUES (".$string.")");
                $this->session->set_userdata('UserInfo',$query);
                $content = "Thank you for your registration!";
                $header = "Your registration is completed!";
                $array_content = array(
                    'Content' => $content, 
                    'Header' => $header);
                $this->load->view('annoucement',$array_content);
                $this->db->query("Delete from "
                        . MYSQL_TABLE_PREFLIX.TABLE_TEMP_USER_ACCOUNTS." "
                        . "where UserEmail = "."'".$query['UserEmail']."'");
            }
        }
        
        public function md5(){
            if($_GET['password']){
                $password = urldecode($_GET['password']);
                echo md5($password);
            }
        }
}
